package org.glut.cloud.repairManage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.glut.cloud.repairManage.service.IRepairTasksService;
import org.glut.cloud.repairManage.entity.RepairTasks;
import org.glut.cloud.repairManage.mapper.RepairTasksMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 维修进度表 服务实现类
 *
 * @author linghuhuolian
 * @since 2024-08-13 15:47
 */
@Service
public class RepairTasksServiceImpl extends ServiceImpl<RepairTasksMapper, RepairTasks> implements IRepairTasksService {

    @Autowired
    private RepairTasksMapper repairTasksMapper;

    /**
     * 查询维修人员的所有任务
     *
     * @param repairPersonnelId
     * @param status
     */
    @Override
    public List<RepairTasks> selectAll(Integer repairPersonnelId, String status) {

        // 1.创建查询条件
        QueryWrapper<RepairTasks> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RepairTasks::getRepairPersonnelId, repairPersonnelId)
                .eq(RepairTasks::getStatus, status);
        // 2.执行查询
        return repairTasksMapper.selectList(queryWrapper);

    }

    /**
     * 接受任务，将repair_task表中的任务状态参数（status）值改为'正在处理',并将接受任务的时间赋值给acceptance_time属性
     *
     * @param taskId
     */
    @Override
    public boolean acceptTask(Integer taskId) {

        // 1.创建更新条件
        UpdateWrapper<RepairTasks> updateWrapper = new UpdateWrapper<>();
        // 2.设置更新条件
        updateWrapper.lambda().eq(RepairTasks::getTaskId, taskId)
                .set(RepairTasks::getStatus, "正在处理")
                .set(RepairTasks::getAcceptanceTime, new Date());
        // 3.执行更新
        int result = repairTasksMapper.update(null, updateWrapper);
        return result == 1;
    }
}
